""" 用于定义图书管理系统的 操作 """
from book_define import Book
from bookshelf_define import BookShelf


class Operation:
    """ 将每个操作都封装成一个对象 """
    def work(self):
        """ 工作方法 """
        pass


class Borrow(Operation):
    """ 借阅图书 """
    def work(self):
        print("--------借阅图书--------")
        print("请输入您所需要借阅的书名：", end='')
        title = input()
        for elem in BookShelf().show_books():
            if title in elem:
                if elem[5] == 'true':
                    print("抱歉，该图书已被借阅！")
                    print("----------------------")
                    return None
                else:
                    book = Book(elem[0], elem[1], title, elem[3], elem[4], 'true')
                    BookShelf().update_into_books(book)
                    BookShelf().insert_into_loan_record(book)   # 添加借阅记录
                    print("借阅成功！")
                    print("----------------------")
                    return None
        print("暂无该藏书！")
        print("----------------------")


class Return(Operation):
    """ 归还图书 """
    def work(self):
        print("--------归还图书--------")
        print("请输入您所需要归还的书名：", end='')
        title = input()
        for elem in BookShelf().show_books():
            if title in elem:
                if elem[5] == 'true':
                    book = Book(elem[0], elem[1], title, elem[3], elem[4], 'false')
                    BookShelf().update_into_books(book)
                    print("归还成功！")
                    print("----------------------")
                    return None
                else:
                    print("该书已在库，无需归还。请检查书名是否正确！")
                    print("----------------------")
                    return None

        print("暂无该藏书！")


class Find(Operation):
    """ 查找图书 """
    def work(self):
        print("--------查找图书--------")
        print("请输入您所需要查找的书名：", end='')
        title = input()
        for elem in BookShelf().show_books():
            if title in elem:
                print(f"%-15s%-20s\t%-25s\t%-14s\t%-22s\t%-12s" % (f"|图书编号：{elem[0]}", f"|图书类型：{elem[1]}", f"|图书名称：《{elem[2]}》", f"|作者：{elem[3]}", f"|出版社：{elem[4]}", f"|图书状态：{'在馆' if elem[5] == 'false' else '已借出'}"))
                print("----------------------")
                return None
        print("暂无该藏书！")
        print("----------------------")


class BookLendingList(Operation):
    """ 图书借阅排行榜 """
    def work(self):
        print("--------借阅排行--------")
        for elem in BookShelf().show_loan_record():
            print(f"图书编号：%-15s\t\t  |图书名称：《{elem[1]}》\t\t   |借阅次数：%-20s" % (elem[0], elem[2]))
        print("----------------------")


class ShelfBook(Operation):
    """ 上架图书 """
    def work(self):
        print("--------上架图书--------")
        print("请输入待上架图书的书籍编号：", end='')
        book_id = input()
        print("请输入待上架图书的图书类型：", end='')
        book_type = input()
        print("请输入待上架图书的图书名称：", end='')
        title = input()
        print("请输入待上架图书的作者：", end='')
        author = input()
        print("请输入待上架图书的出版社：", end='')
        press = input()
        status = 'false'
        for elem in BookShelf().show_books():
            if title in elem:
                print("该图书已上架，请勿重复上架！")
                print("----------------------")
                return None

        book = Book(book_id, book_type, title, author, press, status)
        BookShelf().insert_into_books(book)
        print("上架成功！")
        print("----------------------")


class RemoveOfBooks(Operation):
    """ 下架图书 """
    def work(self):
        print("--------下架图书--------")
        print("请输入待下架的图书书名：", end='')
        title = input()
        book = Book('', '', title, '', '', '')
        for elem in BookShelf().show_books():
            if book.title in elem:
                BookShelf().delete_into_books(book)
                print("下架成功！")
                print("----------------------")
                return None

        print("暂无该藏书！")
        print("----------------------")


class Show(Operation):
    """ 展示所有图书 """
    def work(self):
        print("--------展示图书--------")
        for elem in BookShelf().show_books():
            print(f"%-15s%-20s\t%-25s\t%-14s\t%-22s\t%-12s" % (f"|图书编号：{elem[0]}", f"|图书类型：{elem[1]}", f"|图书名称：《{elem[2]}》", f"|作者：{elem[3]}", f"|出版社：{elem[4]}", f"|图书状态：{'在馆' if elem[5] == 'false' else '已借出'}"))
        print("----------------------")


class Modify(Operation):
    """ 修改图书状态 """
    def work(self):
        print("--------修改图书--------")
        print("请输入待修改图书的图书名称（书名不可修改！）：", end='')
        title = input()
        print("请输入修改后的书籍编号：", end='')
        book_id = input()
        print("请输入修改后的图书类型：", end='')
        book_type = input()
        print("请输入修改后的的作者：", end='')
        author = input()
        print("请输入修改后的出版社：", end='')
        press = input()
        print("请输入修改后的图书的状态：", end='')
        status = input()
        book = Book(book_id, book_type, title, author, press, status)
        for elem in BookShelf().show_books():
            if title in elem:
                BookShelf().update_into_books(book)
                print("修改成功！")
                print("----------------------")
                return None

        print("暂无该藏书，无法完成修改！")
        print("是否上架该图书？\n 1:上架  else：不上架", end='')
        choose = input()
        if choose == '1':
            BookShelf.insert_into_books(book)
            print("上架成功！")
        print("----------------------")


class Exit(Operation):
    """ 退出系统 """
    def work(self):
        print("退出系统")


if __name__ == '__main__':
    test = Show()
    test.work()
